// pages/BossToker/BossToker.js
const app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        startdate: '',//开始时间
        enddate: '',//结束时间
        listdata: [],
        iscustom: false,//是否是自定义选择时间
        nodata: false,
        dateindex: 1,
        total:{
            PerformanceTotal:0,
            CommissionTotal:0,
            TotalCount:0
        },
        param: {
            startDate: '',
            endDate: ''
        }
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var _this = this;
        var time = app.common.getTimeSection(1, "yyyy-MM-dd");
        _this.setData({
            "param": {
                "startDate": time.sdate,
                "endDate": time.edate,
            }
        })
        _this.GetList();
    },
    GetList: function () {
        var _this = this;
        wx.showLoading({
            title: '加载中',
        });
        var param = _this.data.param;
        app.http.POST('/MiniManagerCommission/GetCommissionDetail', param, (err, data) => {
            wx.hideLoading();
            if (data.code == 1) {
                if (data.data == null || data.data.Detail == null || data.data.Detail.length < 1) {
                    _this.setData({
                        "listdata":[],
                        "nodata": true
                    })
                } else {
                    var list = _this.Group(data.data.Detail.reverse());
                    _this.setData({
                        "listdata": list,
                        "nodata": false,
                        "total.PerformanceTotal": data.data.PerformanceTotal,
                        "total.CommissionTotal": data.data.CommissionTotal
                    })
                }
            } else if (data.code == 2) {
                wx.showToast({
                    title: data.msg,
                    icon: 'none',
                    duration: 2000
                })
                _this.setData({
                    "nodata": true
                });
            }
            else {
                wx.showToast({
                    title: '请求失败',
                    icon: 'none',
                    duration: 2000
                })
                _this.setData({
                    "nodata": true
                });
            }
        });
    },
    //JSON数组分组
    Group: function (arr) {
        var map = {},
            dest = [];
        for (var i = 0; i < arr.length; i++) {
            var ai = arr[i];
            ai.BillDate = app.common.dateFormatType(ai.BillDate, 1);
            ai.MemberName = ai.IsMember == true ? ai.MemberName.substring(0, 1) + "XX" : "散客";
            if (ai.ManualCode == "") {
                ai.ManualCode = ai.BillCode;
            } else {
                ai.ManualCode = "手工单号:" + ai.ManualCode;
            }
            if (ai.DetailItemName == "") {
                ai.DetailItemName = this.GetSaleCategory(ai.SaleCategory);
            }
            if (!map[ai.BillID]) {
                dest.push({
                    BillID: ai.BillID,
                    MemberName: ai.MemberName,
                    Sex: ai.Sex,
                    ManualCode: ai.ManualCode,
                    BillDate: ai.BillDate,
                    Detail: [ai]
                });
                map[ai.BillID] = ai;
            } else {
                for (var j = 0; j < dest.length; j++) {
                    var dj = dest[j];
                    if (dj.BillID == ai.BillID) {
                        dj.Detail.push(ai);
                        break;
                    }
                }
            }
        }
        return dest;
    },

    GetSaleCategory: function (Category) {
        var categoryStr = "";
        switch (Category) {
            case 1: categoryStr = "服务"; break;
            case 2: categoryStr = "产品销售"; break;
            case 4: categoryStr = "房间床位收费"; break;
            case 8: categoryStr = "会员卡销售"; break;
            case 16: categoryStr = "会员卡充值"; break;
            case 32: categoryStr = "会员卡取款"; break;
            case 64: categoryStr = "会员卡还款"; break;
            case 128: categoryStr = "顾客退货"; break;
            case 256: categoryStr = "产品消耗"; break;
            case 512: categoryStr = "在线商城"; break;
            case 1024: categoryStr = "套餐充值"; break;
            case 2048: categoryStr = "会员自助充值"; break;
        }
        return categoryStr;
    },
    //日期类型筛选
    DateChange: function (e) {
        var dataset = e.currentTarget.dataset;
        var id = parseInt(dataset.id);
        var iscustom = this.data.iscustom;
        if (id == 4) {//自定义选择时间
            iscustom = true;
            this.setData({
                "iscustom": iscustom,
                "dateindex": id
            });
        }
        else {
            iscustom = false;
            var time = app.common.getTimeSection(id, "yyyy-MM-dd");
            this.setData({
                "iscustom": iscustom,
                "dateindex": id,
                "param.startDate": time.sdate,
                "param.endDate": time.edate
            })
            this.GetList();
        }
    },
    //开始时间筛选
    startDateChange: function (e) {
        this.setData({
            "param.startDate": e.detail.value,
            "startdate": e.detail.value
        });
    },
    //结束时间筛选
    endDateChange: function (e) {
        this.setData({
            "param.endDate": e.detail.value,
            "enddate": e.detail.value
        });
    },
    //自定义筛选
    CustomScreen: function () {
        var _this = this;
        var startdate = _this.data.startdate;
        var enddate = _this.data.enddate;
        if (startdate == "" && enddate == "") {
            wx.showToast({
                title: '请至少选择一个时间',
                icon: 'none',
                duration: 2000
            })
            return;
        }
        _this.GetList();
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
        wx.setNavigationBarTitle({
            title: '员工业绩提成明细'
        })
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})